<?php
class CArticleController {
	public function __construct() {
		$this->db = CDb::getInstance();
	}
	public function getCategories() {
		return $this->db->getResults('SELECT cat_id, cat_name FROM kiss_category WHERE cat_is_show = 1 ORDER BY cat_sort_order ASC');
	}
	public function index() {
		$categories =$this->getCategories();

		$cat_names = array();
		foreach ($categories as $category) {
			$cat_names[$category['cat_id']] = $category['cat_name'];
		}
				
		$articles = $this->db->getResults('SELECT * FROM kiss_article ORDER BY article_modified_time DESC');
		include(APP_PATH.'/view/article_index.php');	
	}
	public function add() {
		$categories = $this->getCategories();
		include(APP_PATH.'/view/article_add.php');	
	}
	public function insert() {
		$_POST['article_created_time'] = NULL;
		$this->db->insert('kiss_article', $_POST);
		$this->index();
	}
	public function edit() {
		$categories = $this->getCategories();
		$article = $this->db->getRow('SELECT * FROM kiss_article WHERE article_id = ?', array($_GET['article_id']));
		include(APP_PATH.'/view/article_edit.php');
	}
	public function update() {
		$this->db->update('kiss_article', $_POST, array('article_id'=>$_POST['article_id']));
		$this->index();
	}
	public function delete() {
		$this->db->delete('kiss_article', array('article_id'=>$_GET['article_id']));
		$this->index();
	}
}
